package com.hjm.project.system.sysConfigJob.service;

import com.hjm.common.constant.UserConstants;
import com.hjm.common.exception.BusinessException;
import com.hjm.common.utils.DateUtils;
import com.hjm.common.utils.StringUtils;
import com.hjm.common.utils.security.ShiroUtils;
import com.hjm.common.utils.text.Convert;
import com.hjm.project.system.job.domain.SysJob;
import com.hjm.project.system.job.mapper.SysJobMapper;
import com.hjm.project.system.sysConfigJob.domain.SysConfigJob;
import com.hjm.project.system.sysConfigJob.mapper.SysConfigJobMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 参数配置 服务层实现
 */
@Service
public class SysConfigJobServiceImpl implements ISysConfigJobService {
    /**
     * 每日交易总额 key
     */
    private static final String DAY_SUM_MONEY = "day_sum_money";

    @Resource
    private SysConfigJobMapper sysConfigJobMapper;
    @Resource
    private SysJobMapper sysJobMapper;

    /**
     * 查询参数配置信息
     *
     * @param configId 参数配置ID
     * @return 参数配置信息
     */
    @Override
    public SysConfigJob selectConfigById(Long configId)
    {
        SysConfigJob config = new SysConfigJob();
        config.setConfigId(configId);
        return sysConfigJobMapper.selectConfig(config);
    }

    /**
     * 根据键名查询参数配置信息
     *
     * @param configKey 参数名称
     * @return 参数键值
     */
    @Override
    public String selectConfigByKey(String configKey)
    {
        SysConfigJob config = new SysConfigJob();
        config.setConfigKey(configKey);
        SysConfigJob retConfig = sysConfigJobMapper.selectConfig(config);
        return StringUtils.isNotNull(retConfig) ? retConfig.getConfigValue() : "";
    }

    /**
     * 查询参数配置列表
     *
     * @param config 参数配置信息
     * @return 参数配置集合
     */
    @Override
    public List<SysConfigJob> selectConfigList(SysConfigJob config)
    {
        return sysConfigJobMapper.selectConfigList(config);
    }

    /**
     * 新增参数配置
     *
     * @param config 参数配置信息
     * @return 结果
     */
    @Override
    public int insertConfig(SysConfigJob config)
    {
        config.setCreateBy(ShiroUtils.getLoginName());
        return sysConfigJobMapper.insertConfig(config);
    }


    /**
     * 更新类型
     * @Param [sysConfigJob]
     * @Return int
     */
    public int updateSysConfigJob(SysConfigJob sysConfigJob) {
        return sysConfigJobMapper.updateSysConfigJob(sysConfigJob);
    }



    /**
     * 修改参数配置
     *
     * @param config 参数配置信息
     * @return 结果
     */
    @Override
    @Transactional
    public void updateConfig(SysConfigJob config) {
        //获取原key
        SysConfigJob sysConfigJob = new SysConfigJob();
        sysConfigJob.setConfigId(config.getConfigId());
        SysConfigJob sysConfigJob1 = sysConfigJobMapper.selectConfig(sysConfigJob);

        config.setUpdateBy(ShiroUtils.getLoginName());
        int i = sysConfigJobMapper.updateConfig(config);
        if (i <= 0) {
            throw new BusinessException("定时任务参数管理更新失败");
        }
        SysJob sysJob = new SysJob();
        sysJob.setJobKey(sysConfigJob1.getConfigKey());
        List<SysJob> sysJobs = sysJobMapper.selectJobList(sysJob);
        if (CollectionUtils.isNotEmpty(sysJobs)) {
            sysJob.setJobId(sysJobs.get(0).getJobId());
            sysJob.setJobKey(config.getConfigKey());
            int k = sysJobMapper.updateJob(sysJob);
            if (k <= 0) {
                throw new BusinessException("定时任务更新任务KEY失败");
            }
        } else {
            throw new BusinessException("定时任务参数相对应的定时任务");
        }
    }

    /**
     * 批量删除参数配置对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteConfigByIds(String ids)
    {
        return sysConfigJobMapper.deleteConfigByIds(Convert.toStrArray(ids));
    }

    /**
     * 校验参数键名是否唯一
     *
     * @param config 参数配置信息
     * @return 结果
     */
    @Override
    public String checkConfigKeyUnique(SysConfigJob config)
    {
        Long configId = StringUtils.isNull(config.getConfigId()) ? -1L : config.getConfigId();
        SysConfigJob info = sysConfigJobMapper.checkConfigKeyUnique(config.getConfigKey());
        if (StringUtils.isNotNull(info) && info.getConfigId().longValue() != configId.longValue())
        {
            return UserConstants.CONFIG_KEY_NOT_UNIQUE;
        }
        return UserConstants.CONFIG_KEY_UNIQUE;
    }


   /**
    * 效验手动执行是否可执行
    * @Param []
    * @Return java.lang.String
    */
    @Override
    public SysConfigJob beginSysConfigJob(String jobType) {
        //查看是否存在自定义统计
        SysConfigJob configJob = new SysConfigJob();
        configJob.setConfigKey(jobType);
        configJob.setConfigType("N");
        //是否存在自定义统计
        List<SysConfigJob> sysConfigJobs = sysConfigJobMapper.selectConfigList(configJob);
        if (CollectionUtils.isNotEmpty(sysConfigJobs)) {
            //修改为执行中
            SysConfigJob sysConfigJob = new SysConfigJob();
            sysConfigJob.setConfigType("Z");
            sysConfigJob.setConfigKey(jobType);
            sysConfigJobMapper.updateSysConfigJob(sysConfigJob);
        } else {
            return new SysConfigJob();
        }
        return sysConfigJobs.get(0);
    }

    /**
     * 结束更新任务状态
     * @Param []
     * @Return java.lang.String
     */
    @Override
    public void endSysConfigJob(String jobType,String msg) {
        //查看是否存在自定义统计
        SysConfigJob configJob = new SysConfigJob();
        configJob.setConfigKey(jobType);
        configJob.setConfigType("Z");
        //是否存在自定义统计
        List<SysConfigJob> sysConfigJobs = sysConfigJobMapper.selectConfigList(configJob);
        if (CollectionUtils.isNotEmpty(sysConfigJobs)) {
            //手动执行才会更新
            SysConfigJob scg = new SysConfigJob();
            //更新结果
            scg.setRemark("当前统计时间:" + DateUtils.getTime() + " 统计结束,统计结果:" + msg);
            scg.setConfigType("Y");
            scg.setConfigKey(jobType);
            int k = sysConfigJobMapper.updateSysConfigJob(scg);
            if (k <= 0) {
                throw new BusinessException("更新任务执行结果状态失败");
            }
        }
    }

}
